/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.esgi.dns;

/**
 *
 * @author Alain Janin-Manificat <alain.janinm@hotmail.fr>
 */
public class TestBinaryOperator {
    public void testBinary(){
        
        /**
         * short 16 bits.
         * [aaaa aaaa bccc deee]
         */
        char a = 'z';
        boolean b =true; //1 bits
        int c = 5;  //3bits
        boolean d =false; //1bits
        int e = 3; //4bits
        
        short encoded =0;
        //0000 0000 0000 0000
        encoded |= (0xFF & a) << 8;
        //0000 0110 0000 0000
        encoded |= (b ? 1 : 0) <<7;
        //0000 0110 1000 0000
        encoded |= (0x7 & c) << 4;
        //0000 0110 1101 0000
        encoded |= (d ? 1 : 0) << 3;
        //0000 0110 1101 0000
        encoded |= (0x7 & e);
        //0000 0110 1101 0011 (1024+512+128+64+16+2+1)
        System.out.println(Integer.toBinaryString(encoded));
        System.out.println(1024+512+128+64+16+2+1 == encoded);
        a='z';

        encoded &= (~0xFF00); //inverting (réinitialisation de champ sur bit cible)
        encoded |= ((0xFF & a)<<8); //réaffectation du champs sur bit.

        System.out.println(encoded);
        
        //Decoding
        System.out.println((char)((0xFF00 & encoded)>>8));
        
        System.out.println(1 == ((128 & encoded)>>7));//print boolean b value
        
        
    }
}
